﻿var questionnairehtml = "Questionnaire/Questionnaire.html";

var contactsCallback_questionnaire = myApp.onPageInit('Questionnaire', function (page) {

    //非微信浏览器下  不显示标题
    //    if (!ISS.utils.isWeiXin()) {
    //        mainView.hideNavbar();
    //        $$('.page-content').attr('style', 'margin-top:-44px')
    //    }

    // 过滤
    $$(".navbar-inner .right img").hide();

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;
    //当前选择的题目
    var currobj = null;

    LoadQuestionnaireInfo();

    function LoadQuestionnaireInfo() {
//        if (iss_localStorage.get("yuqing_appAccount") == "" || iss_localStorage.get("yuqing_appAccount") == null || iss_localStorage.get("yuqing_appAccount") == "undefined") {
//            myApp.alert('appAccount获取参数出错');
//        } else if (iss_localStorage.get("yuqing_appOpenId") == "" || iss_localStorage.get("yuqing_appOpenId") == null || iss_localStorage.get("yuqing_appOpenId") == "undefined") {
//            myApp.alert('appAccount获取参数出错');
//        }
        var _content = JSON.stringify(
            {
                AccountGuid:"b760689cb63a4a92834d6f14173bc293", //iss_localStorage.get("yuqing_appAccount"),
                OpenId: iss_localStorage.get("yuqing_appOpenId"),
                QuestionnaireGuid:"4bf780e165b44e4fa5426f9b10a3b099",// ISS.utils.getParameter("QuestionnaireGuid")
            });
            $$.ajax({
                url: ISS_Constants.Url.const_Questionnaire_get, //ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.FESTIVALS,
                data: { conditionParm: _content },
                crossDomain: true,
                dataType: "json",
                success: function (data, status) {
                    if (data.datas) {
                        var searchTemplate = $$('#template-questionaire-info').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                              var html = "<li class='item-content'>"+
                                        "<div class='item-inner'>"+
                                            "<div class='item-title'  style='width: 40%;' >"+
                                                "姓名"+
                                            "</div>"+
                                            "<input id='xm' type='text' style='height:28px;'>"+
                                        "</div>"+
                                    "</li>"+
                                    "<li class='item-content' >"+
                                        "<div class='item-inner' >"+
                                            "<div class='item-title' style='width: 40%;'>"+
                                                "身份证"+
                                            "</div>"+
                                            "<input id='ids' type='text' style='height:28px;'>"+
                                        "</div>"+
                                    "</li>"+
                                    "<li class='item-content'>"+
                                        "<div class='item-inner'>"+
                                            "<div class='item-title' style='width: 40%;'>"+
                                                "手机号码"+
                                            "</div>"+
                                            "<input id='phone' type='text'  style='height:28px;'>"+
                                        "</div>"+
                                    "</li>";
                             html += compiledTemplate(data);
                          
                            $$("#QItem").html(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                            createanswer(data.datas);
                        }
                    }

                },
                error: function (xhr, status) { }
            });
    }

    save()
})
function initData() {

    var href = window.location.href;
    if (href.indexOf(questionnairehtml) > -1) {
        contactsCallback_questionnaire.trigger();
    }
}

function createanswer(data){
    $$('.open-picker').on('click','a', function () {
        var html = "";
        if ($$('.picker-modal.modal-in').length > 0) {
            myApp.closeModal('.picker-modal.modal-in');
        }
        currobj = $$(this);
        if(data.length>0){
            for(var i=0;i<data.length;i++){
                if(data[i].ProblemGuid == $$(this).parent().attr('problemguid')){
                     html += "<div class='picker-modal picker-1'>"+
                        "<div class='toolbar'>"+
                            "<div class='toolbar-inner'>"+
                                "<div class='left'>"+
                                    "<a href='#' class='close-picker'>取消</a></div>"+
                                "<div class='right'>"+
                                    "<a href='#' ptype='"+ data[i].ProblemType+"' class='close-picker'>完成</a></div>"+
                            "</div>"+
                        "</div>"+
                        "<div class='picker-modal-inner'>"+
                            "<div class='content-block'><ul>";
                    for(var k=0;k<data[i].Stem.length;k++){
                        if(data[i].ProblemType=="2"){
                        html +=("<li><label class='label-checkbox item-content'>"+
                                        "<input {2} type='checkbox' name='my-checkbox' text='{1}' value='{0}' />"+
                                        "<div class='item-media'>"+
                                            "<i class='icon icon-form-checkbox'></i>"+
                                        "</div>"+
                                        "<div class='item-inner'>"+
                                            "<div class='item-title'>"+
                                                "{1}</div>"+
                                        "</div>"+
                                    "</label></li>").format(data[i].Stem[k].StemGuid, data[i].Stem[k].StemName,retattr(data[i].Stem[k].StemGuid));
                        }else{
                            html +=("<li>"+
                                      "<label  class='label-radio item-content'>"+
                                        "<input {2} type='radio' name='my-radio' text='{1}' value='{0}'>"+
                                         "<div class='item-media'>"+
                                            "<i class='icon icon-form-checkbox'></i>"+
                                        "</div>"+
                                        "<div class='item-inner'>"+
                                          "<div class='item-title'>{1}</div>"+
                                        "</div>"+
                                      "</label>"+
                                    "</li>").format(data[i].Stem[k].StemGuid,data[i].Stem[k].StemName,retattr(data[i].Stem[k].StemGuid));
                        }
                    }
                    html += "       </ul></div>"+
                                "</div>"+
                            "</div>";
                }
            }          
        }
        myApp.pickerModal(html);
        selectoption()
    })   
}

function selectoption(){
     $$('.close-picker').click(function(){
        if($$(this).attr('ptype')=="2"){
            var t = "";
            var s = "";
            currobj.find('.checkvalue').html("");
            currobj.find('.checkvalue').attr('val','');
            $$('input[type="checkbox"]:checked').each(function(i,v){
                t+=$$(v).attr('text') + "+";
                s+=$$(v).attr('value') + "+";
            })
            currobj.find('.checkvalue').html(t!=""?t.substring(0,t.length-1):t);
            currobj.find('.checkvalue').attr('val',(s!=""?s.substring(0,s.length-1):s));
            currobj = null;
            }else{
                var t = "";
            var s = "";
            currobj.find('.checkvalue').html("");
            currobj.find('.checkvalue').attr('val','');
            $$('input[type="radio"]:checked').each(function(i,v){
                t+=$$(v).attr('text') + "+";
                s+=$$(v).attr('value') + "+";
            })
            currobj.find('.checkvalue').html(t!=""?t.substring(0,t.length-1):t);
            currobj.find('.checkvalue').attr('val',(s!=""?s.substring(0,s.length-1):s));
            currobj = null;
        }
    })
}   

function retattr(guid)
{
    var attr = "";
    var arr = currobj.find('.checkvalue').attr('val').split('+');
    for(var i=0;i<arr.length;i++){
        if(arr[i] == guid){
            attr="checked='checked'";
        }
    }
    return attr;
}       

function save(){
    $$('.save').click(function(){
        var AccountGuid="b760689cb63a4a92834d6f14173bc293"; //iss_localStorage.get("yuqing_appAccount"),
        var OpenId= "oVd1auLN6qa1lIaTwrNIXyxsc9_M" //iss_localStorage.get("yuqing_appOpenId");
        var QuestionnaireGuid="4bf780e165b44e4fa5426f9b10a3b099";// ISS.utils.getParameter("QuestionnaireGuid")
        if(AccountGuid!="" && OpenId!="" && QuestionnaireGuid!=""){
            var array = new  Array();
            var _content = "";
            $$('#QItem li').each(function(i,v){
                var ProblemGuid =  $$(v).attr('problemguid')
                if(ProblemGuid != null && ProblemGuid !=""){
                var guids = $$(v).find('.checkvalue').attr('val').split('+');
                    for(var z=0;z<guids.length;z++){
                        var Questionnaire_View_Para = new Object();
                        Questionnaire_View_Para.AccountGuid = AccountGuid
                        Questionnaire_View_Para.OpenId = OpenId
                        Questionnaire_View_Para.QuestionnaireGuid = QuestionnaireGuid
                        Questionnaire_View_Para.ProblemGuid = ProblemGuid
                        Questionnaire_View_Para.StemGuid = guids[z]
                        array.push(Questionnaire_View_Para);
                    }
                     _content = array;
                }
            })
             $$.ajax({
                url: ISS_Constants.Url.const_Questionnaire_add, //ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.FESTIVALS,
                data: { conditionParm: JSON.stringify(_content) },
                crossDomain: true,
                dataType: "json",
                Type:"post",
                success: function (data, status){
                },
                 error: function (xhr, status) { }
            });
        }
    })
}
 
initData();